package cn.tedu.fy.pethome.controller;

import cn.tedu.fy.pethome.ex.ServiceException;
import cn.tedu.fy.pethome.pojo.dto.ChangePasswordDTO;
import cn.tedu.fy.pethome.pojo.dto.UserRegistryDTO;
import cn.tedu.fy.pethome.service.IUserServiceImpl;
import cn.tedu.fy.pethome.util.JwtTokenUtils;
import cn.tedu.fy.pethome.util.LoginUtils;
import cn.tedu.fy.pethome.web.JsonResult;
import cn.tedu.fy.pethome.web.ServiceCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/ums/user")
@Slf4j
@Api(tags = "用户信息功能")
public class UserController {

    @Autowired
    private IUserServiceImpl userService;
    @Autowired
    private JwtTokenUtils jwtTokenUtils;

    /**
     * 注册用户
     * @param userRegistryDTO
     * @return
     */
    @ApiOperation(value = "注册用户")
    @PostMapping("/register")
    @PreAuthorize("permitAll()")
    public JsonResult doRegister(UserRegistryDTO userRegistryDTO){
        userService.doRegister(userRegistryDTO);
        return JsonResult.ok();
    }

    @ApiOperation(value="验证手机,邮箱,用户名是否重复")
    @PostMapping("/checkValue")
    @PreAuthorize("permitAll()")
    @ApiImplicitParams({
            @ApiImplicitParam(value="验证值",name="value",type = "string"),
            @ApiImplicitParam(value="验证值类型,phone,username,email",name="type",type = "string")
    })
    public JsonResult checkValue(String value,String type) {
        userService.checkValue(value, type);
        return JsonResult.ok();
    }
    @Value("${jwt.tokenHead}")//对未配置或值为空的属性设置默认值。
    private String tokenHead;//Bearer

    /**
     * 修改密码
     */

    @ApiOperation(value = "修改登录密码")
    @PostMapping("/renew/password")
    @PreAuthorize("hasRole('ROLE_user')")
    public JsonResult renrenewPassword(@Valid ChangePasswordDTO changePasswordDTO,
                                       HttpServletRequest request,
                                       @RequestHeader("Authorization") String authToken) {
        String ip = LoginUtils.getIpAddress(request);
        changePasswordDTO.setIp(ip);
        //获取来访者地址
        String userAgent = request.getHeader("User-Agent");
        changePasswordDTO.setUserAgent(userAgent);
        String token = getToken(authToken);
        userService.renewPassword(changePasswordDTO, token);
        return JsonResult.ok();

    }

    public String getToken(String authToken){
        if (authToken==null||!(authToken.startsWith("Bearer "))){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"无法从请求中拿到token");
        }
        return authToken.substring(tokenHead.length());
    }

}
